<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="父标签"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          extra="父标签"
        >
          <a-select v-decorator="['status', {initialValue: 1}]">
            <a-select-option
              v-for="(el, index) in statusArray"
              :key="index"
              :value="el.value"
            >{{ el.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="标签名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          extra=""
        >
          <a-input v-decorator="['name', {rules: [{required: true, message: '请输入标签名称'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import * as Api from '@/api/user'
import { InputNumberGroup } from '@/components'

// 父标签数组
const statusArray = [
  {name: '店铺', value: 1},
  {name: '个人', value: 2}
]

export default {
  components: {
    InputNumberGroup
  },
  data () {
    return {
      // 对话框标题
      title: '新增会员等级',
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: { span: 13 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 父标签数组
      statusArray,

      // 当前记录
      record: {}
    }
  },
  created () {

  },
  methods: {

    /**
     * 显示对话框
     */
    add () {
      // 显示窗口
      this.visible = true
      this.record = {}
    },

    // 显示对话框
    handle (record) {
      // 显示窗口
      this.visible = true
      // 当前记录
      this.record = record
      // 设置默认值
      this.setFieldsValue()
    },
    
    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { record, form: { setFieldsValue } } = this
      this.$nextTick(() => {
        setFieldsValue(pick(record, ['id', 'name', 'status']))
        console.log(this.form)
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
      this.form.resetFields()
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.confirmLoading = true
      if (this.record.id) { // 有id, 修改接口
        Api.tagUpdate({ id: this.record.id, form: values })
          .then((result) => {
            // 显示成功
            this.$message.success(result.message, 1.5)
            // 关闭对话框事件
            this.handleCancel()
            // 通知父端组件提交完成了
            this.$emit('handleSubmit', values)
          })
          .finally(() => {
            this.confirmLoading = false
          })
      } else { // 新增接口
        Api.tagAdd({ form: values })
          .then((result) => {
            // 显示成功
            this.$message.success(result.message, 1.5)
            // 关闭对话框事件
            this.handleCancel()
            // 通知父端组件提交完成了
            this.$emit('handleSubmit', values)
          })
          .finally(() => {
            this.confirmLoading = false
          })
      }
    }

  }
}
</script>
